package it.hopeless.jee6.registry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static javax.ejb.ConcurrencyManagementType.BEAN;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.ConcurrencyManagement;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;

/**
 * User: frico
 * Date: Feb 6, 2010
 * Time: 7:15:40 PM
 */
@Singleton
@ConcurrencyManagement(BEAN)
@Startup
public class RegistryService {
    private static final Logger logger = LoggerFactory.getLogger(RegistryService.class);
    private final Map<String,String> properties = new TreeMap<String,String>();

    @PostConstruct
    public void applicationStartup() {
        logger.info("Loading System properties into Registry");
        properties.putAll((Map) System.getProperties());
    }

    @PreDestroy
    public void applicationShutdown() {
        logger.info("Clearing Application Registry");
        properties.clear();
    }

    public String getProperty(String key) {
        return properties.get(key);
    }

    public String setProperty(String key, String value) {
        logger.info("Setting (modifying)  key \"{}\" with value: \"{}\" into Registry",key,value);
        return properties.put(key, value);
    }

    public String removeProperty(String key) {
        logger.info("Removing  key \"{}\" with value: \"{}\" from Registry",key,properties.get(key));
        return properties.remove(key);
    }

    public void putAll(Properties properties){
        logger.info("Adding Application properties into Registry");
        this.properties.putAll((Map) properties);
    }

    public Map<String, String> getProperties(){
        return properties;
    }

}
